// pages/ucenter/personal/index.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: '',
      avatarUrl: '',
      avatar: '',
      realName: '',
      mobile: '',
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChooseAvatar(e) {
    console.log(e)
    this.upload(e.detail.avatarUrl);
  },

  bindNameInput(e) {
    let data = this.data.userInfo;
    data.nickName = e.detail.value;
    this.setData({
      userInfo: data
    });

  },


  upload: function (filePath) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    const uploadTask = wx.uploadFile({
      url: api.StorageUpload,
      filePath: filePath,
      name: 'file',
      success: function (res) {
        console.log(res.data)
        let resData = JSON.parse(res.data);
        console.log(resData);
        wx.hideLoading();
        if (resData.errno == 0) {
          console.log("errno == 0");
          let data = that.data.userInfo;
          data.avatarUrl = resData.data.url;
          that.setData({
            userInfo: data
          })
        } else {
          console.log("errno !=  0")
        }
      },
      fail: function (e) {
        wx.hideLoading();
        wx.showModal({
          title: '错误',
          content: '上传失败',
          showCancel: false
        })
      },
    })

    uploadTask.onProgressUpdate((res) => {
      console.log('上传进度', res.progress)
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
    })

  },

  getUserInfo() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });

    util.request(api.userInfoUrl, 'GET').then(function (res) {
      if (res.errno === 0) {
        let data = res.data;
        that.setData({
          userInfo: data,
        })

      }
      wx.hideLoading();
    }).catch(err => {
      wx.hideLoading();
    });
  },



  saveUserInfo: function () {
    let that = this;

    if (null == that.data.userInfo.nickName || that.data.userInfo.nickName == '') {
      util.showErrorToast("请填写昵称");
      return;
    }

    if (null == that.data.userInfo.avatarUrl || that.data.userInfo.avatarUrl == '') {
      util.showErrorToast("请选择头像");
      return;
    }


    wx.showLoading({
      title: '加载中',
    });

    that.data.userInfo.avatar = that.data.userInfo.avatarUrl;
    that.data.userInfo.nickname = that.data.userInfo.nickName;

    util.request(api.setUserInfoUrl, this.data.userInfo).then(function (res) {
      if (res.errno == 0) {
        //重新获取用户信息
        user.getUserInfoServer().then(res => {
          wx.hideLoading();
          wx.navigateBack();
        }).catch(err => {
          wx.hideLoading();
        });
      }

    }).catch(err => {
      wx.hideLoading();
    });


  },
})